(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.account.deleteVerifyController', editController);
	editController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'accountService' ];
	function editController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, accountService) {
		var ctrl = this, params = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.account = {};
		ctrl.accountOld = {};
		initAcctCcyView();
		ctrl.isShowAcctCcyList = function(){
			if(ctrl.account && ctrl.account.custAcctFlag == 'Y'){
				return false;
			}else{
				return true;
			}
		};
		
		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedParamList = selectedRows;
		}
		
		ctrl.subMoreDataLookup = function (obj){
			modalService.openCustomDialogs({
			   'title': 'title.account.moredata',
			   'dialogsModule' : 'cda.acctMoreDataViewLookup',
			   'params': obj,
			   'btnCallback' : {
					'ok' : function(resultList) {
					}
				}	
			});
		};
		
		ctrl.splitAccountLookup = function (){
			var params = {};
			if(ctrl.account && ctrl.account.splitAcct && ctrl.account.splitAcct.length > 0){
				 for(var i =0; i < ctrl.account.splitAcct.length; i++){
					 ctrl.account.splitAcct[i].dataStatus = 'V';
				 }
			}
			params.splitAcct = ctrl.account.splitAcct ||[];
			params.patternflag = ctrl.account.patternflag;
			modalService.openCustomDialogs({
			   'title': 'title.account.splitacct.lookup',
			   'dialogsModule' : 'cda.splitAccountViewLookup',
			   'params':params,
			   'btnCallback' : {
					'ok' : function(resultList) {
						ctrl.account.splitAcct = resultList.splitAcct ;
						ctrl.account.patternflag = resultList.patternflag;
					}
				}	
			});
		}; 

		ctrl.moreDataLookup = function (){
			modalService.openCustomDialogs({
			   'title': 'title.account.moredata',
			   'dialogsModule' : 'cda.acctMoreDataViewLookup',
			   'params': ctrl.account,
			   'btnCallback' : {
					'ok' : function(resultList) {
					}
				}	
			});
		};

		ctrl.init = function() {
			ctrl.innerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.acctCcyList.tableConfigs.table, rowSelectionChanged);
			ctrl.innerGridOptions.data = 'vm.account.acctCcyList';
			ctrl.innerGridOptions.enableRowHeaderSelection = false;
			authService.deleteVerify(params).then(function(response) {
				accountService.initOptions().then(function(result){
					//Initialization data
					ctrl.custAcctFlagOptions = result.custAcctFlagOptions;
					ctrl.acctPatternFlagOptions = result.acctPatternFlagOptions;
					ctrl.splitFlagOptions = result.splitFlagOptions;
					ctrl.summaryFlag1Options = result.summaryFlag1Options;
					ctrl.summaryFlag2Options = result.summaryFlag2Options;
					ctrl.acctTypeOptions = result.acctTypeOptions;
					ctrl.partyTypeOptions = result.partyTypeOptions;
					ctrl.acctCcyOptions = result.acctCcyOptions;
					ctrl.acctNoOptions = result.acctNoOptions;
					ctrl.accountPatterns = result.accountPatterns;
					ctrl.accountPatternsTwo = result.accountPatternsTwo;
					
					ctrl.account = response.data.model || {};
					ctrl.account.taskId = response.data.task.taskId;
					ctrl.account.acctCcyList = ctrl.account.acctCcyList || [];
				});
			});
		};

		ctrl.deleteVerifyReject = function() {
			modalService.openPromptDialogs({
				'btnCallback' : {
					'ok' : function(comments) {
						ctrl.account.comments = comments;
						authService.deleteVerifyReject(ctrl.account).then(rejectSuccessful, rejectFailed);
					}
				}
			});
		};

		ctrl.deleteVerifyApprove = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.approve', args : [ctrl.account.primaryKey]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.deleteVerifyApprove(ctrl.account).then(approveSuccessful, approveFailed);
					}
				}
			});
		};

		function initAcctCcyView(){
			ctrl.viewAcctCcy = function(row) {
				var acctCcyObjOld = getAcctCcyObjByCcy(row.entity.acctCcy);
				modalService.openCustomDialogs({
				   'title': 'title.account.acctccy.view',
				   'dialogsModule' : 'cda.account.accountCcyView',
				   'params': {acctCcy : row.entity, acctCcyOld : acctCcyObjOld},
				   'btnCallback' : {
					}
				});
			};
		};
		
		function getAcctCcyObjByCcy(acctCcy){
			var result = {};
			if(ctrl.accountOld && ctrl.accountOld.acctCcyList && ctrl.accountOld.acctCcyList.length > 0){
				angular.forEach(ctrl.accountOld.acctCcyList, function(data, index, array){
					if(data.acctCcy == acctCcy){
						result = data;
					}
				});
			}
			return result;
		}
		
		function approveSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.approve' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.account.list',
					'state' : 'cda.account.list',
					'params' : {}
				}]
			});
		}

		function approveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function rejectSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.reject' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.account.list',
					'state' : 'cda.account.list',
					'params' : {}
				}]
			});
		}

		function rejectFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);
